#ifndef POPUP_THREAD_WIDGET_HPP
#define POPUP_THREAD_WIDGET_HPP

#include <QtGui>
#include <PopupLibTypes.hpp>
#include "PopupCustomButton.hpp"
#include "PopupUsersManager.hpp"
#include "PopupUserProfileFrame.hpp"
#include "ui_threadwidget.h"

struct PopupThreadTextWidget;

struct PopupThreadWidget : public QWidget, PopupUsersObserver, private Ui_ThreadWidget
{
  Q_OBJECT

public:
  PopupThreadWidget(QWidget *p_parent,
                    const Popup::ThreadID & p_threadID,
                    const Popup::UserList & p_attendants,
                    const QString & p_threadTitle,
                    bool p_amIThreadStarter,
                    const QString & p_text = "");
  virtual ~PopupThreadWidget();

  void processMessageReaction(const Popup::RateUserMessage & p_reaction);
  void processInvitation(const Popup::ThreadInvitation & p_invitation,
                            bool p_isCc);

  void userUpdated(const PopupUserExt* p_user,
                     unsigned short p_updateMask,
                     bool p_isNewUser);

  const Popup::ThreadID & getID() const;
  const QString & getTitle() const;
  QStringList getUsers() const;
  QString getText() const;
  bool doThreadTitleUpdate(const QString & p_title);

signals:
  void signal_sendMessage(Popup::UserMessage *p_message);
  void signal_sendMessageReaction(Popup::RateUserMessage *p_reaction);
  void signal_replyMessage(Popup::UserMessage *p_message, bool _replyAll);
  void signal_showImage(const QString & p_image, const QString & p_sender = "");
  void signal_sendInvitation(Popup::ThreadInvitation *p_invitation);

public slots:
  void slot_showMessage(const Popup::UserMessage *p_message);
  void slot_sendMessage();
  void slot_onUrlClicked(const QUrl & p_url);
  void slot_onMouseOverUrl(const QString & p_url);
  void slot_applyNewSettings();

private slots:
  void slot_browseAttachment();
  void slot_showAttendantsList(bool p_show);
  void slot_enterPrivateMessageMode(QListWidgetItem *p_itemClicked);
  void slot_exitPrivateMessageMode();
  void slot_addAttendants();
  void slot_showUserProfile();
  void slot_customButtonClicked();

private:
  void addAttendant(Popup::UserID p_userID, bool p_notify);
  QString eventNotificationString(const QString & p_notif,
                                     const QColor & p_textColor);

  typedef unsigned short MsgReactionMask;
  typedef std::map<Popup::MessageID, MsgReactionMask> Message2ReactionsMap;
  typedef std::map<QAction*, Popup::UserRateItem> Action2ReactionMap;
  typedef std::map<Popup::UserRateItem, QAction*> Reaction2ActionMap;
  typedef std::map<Popup::MessageID, int> Message2BlocknumberMap;

  Popup::UserMessage *m_currentMessage;
  QPoint m_dragPosition;
  Popup::ThreadID m_threadID;
  PopupThreadTextWidget *m_threadText;
  QString m_threadTitle;
  bool m_sendThreadTitle;
  Message2ReactionsMap m_message2ReactionsMap;
  Action2ReactionMap m_action2ReactionMap;
  Reaction2ActionMap m_reaction2ActionsMap;
  Popup::UserID m_selectedUserForPrivateMessage;
  PopupUserProfileFrame *m_userProfileFrame;
  QTimer m_userProfileFrameShowTimer;
  Popup::UserID m_userProfileFrameShowUser;
  Popup::UserList m_unresolvedUsers;

  QMenu m_contextMenu;
  QAction *m_likeAction;
  QAction *m_dontLikeAction;
  QAction *m_pointFinesseAction;
  QAction *m_fuckAction;

  QTextDocument* document;
  QTextCursor*   cursor;
  int currentMessageIndex;
  Message2BlocknumberMap m_message2BlocknumberMap;
  PopupCustomButtonList m_customButtonList;
};

#endif // POPUP_THREAD_WIDGET_HPP
